<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <el-form :inline="true" class="demo-form-inline">
          <el-row>
            <el-col :span="8">
              <el-form-item label="学科名称">
                <el-input v-model="query.keywrd"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-button type="primary" @click="quxiao">清除</el-button>
              <el-button type="primary" @click="queding">搜索</el-button>
            </el-col>
            <el-col :span="8" style="text-align: right">
              <el-form-item>
                <el-button @click="zengjia" type="success" icon="el-icon-edit"
                  >新增按钮</el-button
                >
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>

        <!-- 表格 -->
        <el-table :data="formInline" style="width: 100%">
          <el-table-column type="index" label="序号"> </el-table-column>
          <el-table-column prop="subjectName" label="学科名称">
          </el-table-column>
          <el-table-column prop="username" label="创建者"> </el-table-column>
          <el-table-column label="创建日期">
            <template #default="{ row }">
              {{ formatDate(row.addDate) }}
            </template>
          </el-table-column>
          <el-table-column label="前台是否显示">
            <template #default="{ row }">
              {{ formart(row.isFrontDisplay) }}
            </template>
          </el-table-column>
          <el-table-column prop="twoLevelDirectory" label="二级目录">
          </el-table-column>
          <el-table-column prop="tags" label="标签"> </el-table-column>
          <el-table-column prop="totals" label="数量"> </el-table-column>
          <el-table-column label="操作" width="240px" fixed="right">
            <template #default="{ row }">
              <el-button type="text" @click="skip(row.id)">学科分类</el-button>
              <el-button type="text" @click="biaoqian(row.id)"
                >学科标签</el-button
              >
              <el-button type="text" @click="modifyAdd(row.id)">修改</el-button>
              <el-button type="text" @click="fetchDelRole(row.id)"
                >删除</el-button
              >
              <!--  -->
            </template>
          </el-table-column>
        </el-table>

        <el-pagination
          style="text-align: right; margin-top: 20px; margin-bottom: 15px"
          background
          :total="total"
          :page-size="query.pagesize"
          :current-page="query.page"
          @current-change="handleCurrentChange"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
        >
        </el-pagination>
      </el-card>
    </div>
    <el-dialog
      :title="`${ruleForm.id ? '修改' : '新增'}`"
      :visible="roleDialogVisible"
      @close="closeRoleDialog"
    >
      <!-- 表单内容 -->
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="学科名称" prop="subjectName">
          <el-input v-model="ruleForm.subjectName"></el-input>
        </el-form-item>
        <el-form-item label="是否显示" prop="delivery">
          <el-switch v-model="delivery"></el-switch>
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-button size="small" @click="guanbi">取消</el-button>
        <el-button size="small" type="primary" @click="confirmAdd"
          >确定</el-button
        >
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { list, add, remove, detail, update } from '@/api/hmmm/subjects.js'
import dayjs from 'dayjs'
export default {

  data () {
    return {
      ruleForm: {
        subjectName: '',
        isFrontDisplay: 1

      },
      delivery: false,
      rules: {
        subjectName: [
          { required: true, message: '学科名称不能为空', trigger: 'blur' },
          { min: 1, max: 5, message: '长度在 1 到 5 个字符', trigger: 'blur' }
        ]
      },
      formInline: [],

      query: {
        keyword: '',
        page: 1,
        pagesize: 10
      },
      total: 0,
      roleDialogVisible: false

    }
  },
  //
  created () {
    this.huoqu()
  },
  methods: {
    async huoqu () {
      const res = await list(this.query)
      // console.log(res)
      this.formInline = res.data.items
      this.total = res.data.counts
    },
    quxiao () {

    },
    queding () {

    },
    handleCurrentChange (page) {
      this.query.page = page
      this.huoqu()
    },
    handleSizeChange (val) {
      this.query.pagesize = val
      this.huoqu()
    },
    // 打开弹窗
    zengjia () {
      this.roleDialogVisible = true
      this.ruleForm = {
        subjectName: '',
        isFrontDisplay: 1
      }
    },
    // 关闭弹窗
    closeRoleDialog () {
      this.roleDialogVisible = false
    },
    // 新增
    confirmAdd () {
      this.$refs.ruleForm.validate(async vaild => {
        if (vaild) {
          if (this.ruleForm.id) {
            await update(this.ruleForm)
          } else {
            await add(this.ruleForm)
          }

          this.huoqu()
          this.$message.success(`${this.ruleForm.id ? '修改' : '新增'}成功`)

          this.roleDialogVisible = false
        }
      })
    },
    guanbi () {
      this.roleDialogVisible = false
      this.ruleForm = {
        subjectName: ''

      }
    },
    // 删除
    fetchDelRole (id) {
      this.$confirm('此操作将永久删除该学科, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // 调用删除接口
        await remove({ id })
        // 更新列表
        this.huoqu()
        // 提示用户
        this.$message.success('删除成功')
      }).catch()
    },
    // 修改
    async modifyAdd (id) {
      this.roleDialogVisible = true
      const res = await detail({ id })
      console.log(res)
      this.ruleForm = res.data
    },
    skip (id) {
      this.$router.push({
        path: '/subjects/directorys',
        query: {
          subjectID: id
        }
      })
    },
    biaoqian (id) {
      this.$router.push({
        path: '/subjects/tags',
        query: {
          subjectID: id
        }
      })
    },
    // 格式化前台是否显示
    formart (value) {
      const TYPES = {
        1: '是',
        0: '否'
      }
      return TYPES[value]
    },
    // 格式化时间
    formatDate (value, str = 'YYYY-MM-DD') {
      return dayjs(value).format(str)
    }

  }
}
</script>

<style scoped></style>
